<template>
	<view class="home">
		<custom-navbar title="人才需求" :show-back="true" @back="handleBack" />

		<view class="content" :style="{ paddingTop: navBarHeight + 'px' }">
			<!-- 空状态 -->
			<view v-if="isEmpty" class="empty-state fcolcc">
				<image class="empty-image" src="/static/images/common/empty.png"></image>
				<view class="empty-text">暂无人才需求数据</view>
				<up-button type="primary" shape="circle" @click="loadRecruitmentList(true)"
					style="width: 200rpx;margin-top: 30rpx;">
					重新加载
				</up-button>
			</view>

			<!-- 人才需求列表 -->
			<view v-else class="recruitment-list p30">
				<view class="item p30 mb20" v-for="(item,index) in recruitmentList" :key="index"
					@click="handleDetails(item)">
					<view class="fcsb mb18">
						<view class="f36bc0">{{item.position_name}}</view>
						<view class="f36bcFF0C10">{{item.salary_range_text}}</view>
					</view>
					<view class="f225c0 mb18">{{item.enterprise_id_text}}·{{item.enterprise_size_text}}人</view>
					<view class="fc mb30">
						<view class="condition mr10 p0020">
							<view>{{item.province_id_text}}</view>
						</view>
						<view class="condition mr10 p0020">
							<view>{{item.work_experience_text}}</view>
						</view>
						<view class="condition mr10 p0020">
							<view>{{item.education_requirement_text}}</view>
						</view>
					</view>
					<up-line></up-line>
					<view class="f22c9 mt30">{{formatDate(item.create_time)}}发布</view>
				</view>
			</view>

			<!-- 加载更多 -->
			<view v-if="isLoadingMore" class="loading-more fcc">
				<up-loading-icon text='加载中...'></up-loading-icon>
			</view>
			<view v-if="noMoreData" class="no-more fcc">
				没有更多数据了
			</view>
		</view>
	</view>
</template>

<script setup>
	import CustomNavbar from '@/components/custom-navbar.vue'
	import {
		ref,
		onMounted,
		computed
	} from 'vue';
	import {
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		myRecruitment
	} from '@/api/user'

	// 系统信息
	const systemInfo = uni.getSystemInfoSync()
	const navBarHeight = computed(() => systemInfo.statusBarHeight + 44)

	// 数据状态
	const isLoading = ref(false)
	const isLoadingMore = ref(false)
	const noMoreData = ref(false)
	const isEmpty = ref(false)

	// 分页参数
	const page = ref(1)
	const limit = ref(10)
	const total = ref(0)

	// 人才需求列表数据
	const recruitmentList = ref([])

	// 初始化加载
	onMounted(() => {
		loadRecruitmentList(true)
	})

	// 加载人才需求列表
	const loadRecruitmentList = async (isRefresh = false) => {
		if (isLoading.value && !isRefresh) return

		try {
			if (isRefresh) {
				page.value = 1
				noMoreData.value = false
				isLoading.value = true
			} else {
				isLoadingMore.value = true
			}

			const params = {
				page: page.value,
				limit: limit.value
			}

			const res = await myRecruitment(params)

			if (res.code === 200) {
				if (isRefresh) {
					recruitmentList.value = res.data.data
					total.value = res.data.total
				} else {
					recruitmentList.value = [...recruitmentList.value, ...res.data.data]
				}

				// 检查是否还有更多数据
				if (res.data.data.length < limit.value) {
					noMoreData.value = true
				}

				// 检查空状态
				isEmpty.value = recruitmentList.value.length === 0
			}
		} catch (error) {
			console.error('加载人才需求列表失败:', error)
			uni.showToast({
				title: '加载失败，请重试',
				icon: 'none'
			})
		} finally {
			isLoading.value = false
			isLoadingMore.value = false
		}
	}

	// 格式化日期
	const formatDate = (dateString) => {
		if (!dateString) return ''
		const date = new Date(dateString)
		return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`
	}

	// 返回
	const handleBack = () => {
		uni.switchTab({
			url: '/pages/user/user'
		})
	}

	// 查看详情
	const handleDetails = (item) => {
		uni.navigateTo({
			url: `/pages/talents/position?recruitment_id=${item.recruitment_id}`
		})
	}

	// 加载更多
	onReachBottom(() => {
		if (isLoadingMore.value || noMoreData.value) return
		page.value += 1
		loadRecruitmentList()
	})
</script>

<style lang="scss" scoped>
	.home {
		width: 100%;
		min-height: 100vh;
		background-color: #f7f7f7;

		.content {
			padding-top: var(--status-bar-height);
			padding-top: calc(var(--status-bar-height) + 70rpx);

			.empty-state {
				height: 60vh;
				padding: 40rpx;
				text-align: center;

				.empty-image {
					width: 300rpx;
					height: 300rpx;
					margin-bottom: 30rpx;
				}

				.empty-text {
					font-size: 28rpx;
					color: #999;
				}
			}

			.recruitment-list {
				.item {
					width: 100%;
					background: #FFFFFF;
					border-radius: 20rpx;
					border: 1rpx solid;
					border-image: linear-gradient(61deg, rgba(57, 141, 251, 0), rgba(255, 255, 255, 1)) 1 1;
					margin-bottom: 20rpx;

					.f36bc0 {
						font-size: 36rpx;
						font-weight: bold;
						color: #333;
					}

					.f36bcFF0C10 {
						font-size: 36rpx;
						font-weight: bold;
						color: #FF0C10;
					}

					.f225c0 {
						font-size: 22rpx;
						color: #5c5c5c;
					}

					.condition {
						height: 44rpx;
						background: rgba(146, 146, 146, 0.1);
						border-radius: 8rpx;
						font-size: 20rpx;
						color: #A1A1A1;
						line-height: 44rpx;
						padding: 0 20rpx;
					}

					.f22c9 {
						font-size: 22rpx;
						color: #999;
					}
				}
			}

			.loading-more,
			.no-more {
				padding: 30rpx 0;
				color: #999;
				font-size: 26rpx;
			}
		}
	}

	.fcolcc {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.fcsb {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.fc {
		display: flex;
		align-items: center;
	}

	.p30 {
		padding: 30rpx;
	}

	.p0020 {
		padding: 0 20rpx;
	}

	.mb18 {
		margin-bottom: 18rpx;
	}

	.mb20 {
		margin-bottom: 20rpx;
	}

	.mb30 {
		margin-bottom: 30rpx;
	}

	.mt30 {
		margin-top: 30rpx;
	}

	.mr10 {
		margin-right: 10rpx;
	}
</style>